//
//  BkConfirmViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/8.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BkConfirmViewController.h"
#import "BkDuihuanSucessViewController.h"
#import "LXAddressListViewController.h"
#import "BkOrderConfiromView.h"
#import "LXMarketBottomView.h"
#import "LXAddressModel.h"

@interface BkConfirmViewController ()
@property (nonatomic,strong) BkOrderConfiromView *headerView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) LXMarketBottomView *bottomView;
@property (nonatomic,strong) LXAddressModel *addressModel;
@end

@implementation BkConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单确认";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self getmyAddressListRequst];
}
-(BkOrderConfiromView *)headerView{
    if (!_headerView) {
        _headerView = [[BkOrderConfiromView alloc]init];
        WS(weakSelf);
        _headerView.dataDic = _dataDic;
        _headerView.selcetAddressBlock = ^(UILabel * _Nonnull label) {
            LXAddressListViewController *vc = [[LXAddressListViewController alloc]init];
            vc.type = AddressListTypeEdting;
            vc.callBackBlock = ^(LXAddressModel * _Nonnull model) {
                weakSelf.addressModel = model;
            };
            [weakSelf naviPushVc:vc];
        };
    }
    return _headerView;
}
-(LXMarketBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[LXMarketBottomView alloc]init];
        _bottomView.top = _tableView.bottom;
        WS(weakSelf);
       NSString* newprice = self.dataDic[@"newprice"];
        if ([kIsmember intValue] == 0) {
            newprice = self.dataDic[@"oldprice"];
        }
        _bottomView.priceLabel.text = [NSString stringWithFormat:@"%@积分",newprice];
        _bottomView.conformBuyBlock = ^{
            [weakSelf exchangeintegralgoodsRequst];
           
        };
        
    }
    return _bottomView;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth , TSScreenHeight -  Height_TabBar - TSScaleW(50)) style:(UITableViewStyleGrouped)];
       
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        _tableView.tableHeaderView = self.headerView;
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
       
    }
    return _tableView;
}
#pragma mark -----请求我的地址
-(void)getmyAddressListRequst
{

 NSDictionary * pamas = @{@"nowPage":[NSString stringWithFormat:@"%@",@"1"],@"pageCount":[NSString stringWithFormat:@"%d",kPage_Size]};
    [NetWorkTools postConJSONWithUrl:@"myAddressList" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
        NSArray *array = responseObject[@"dataList"];
       
        if (result.integerValue == 0) {
            for (NSDictionary *dic  in array) {
                NSString *isdefault = dic[@"isdefault"];
                if (isdefault.integerValue == 1) {
                    LXAddressModel *model = [[LXAddressModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    self.addressModel = model;
                    break;
                }
            }
            
        }else{
             [MBProgressHUD showError:resultNote];
        }
         
    } fail:^(NSError *error) {
        
    }];
}
-(void)setAddressModel:(LXAddressModel *)addressModel{
    _addressModel = addressModel;
    NSString *address = [NSString stringWithFormat:@"%@%@",_addressModel.address,_addressModel.addressdetail];
    self.headerView.addressLabel.text = address;
    
}
-(BOOL)pamasIsOkOrNot{
   
       if (!self.dataDic) {
          // [SSTool error:SSKJLanguage(@"请输入您的手机号")];
           [MBProgressHUD showError:@"请重新加载数据"];
           return NO;
       }
    if (!self.addressModel.addressid.length) {
        [MBProgressHUD showError:@"请选择收货地址"];
        return NO;
    }
       
       
     
    return YES;
}
#pragma mark ----商品立即购买
//cmd    是    string    immediatelypurchase
//uid    是    string    用户id
//gid    是    string    商品id
//numbers    是    string    购买数量
//addressid    是    string    收货地址id
//remarks    否    string    备注
#pragma mark ------积分商品兑换
-(void)exchangeintegralgoodsRequst
{
    if (![self pamasIsOkOrNot]) {
        return;
    }
    
    NSDictionary * pamas = @{@"gid":_dataDic[@"gid"],@"numbers":[NSString stringWithFormat:@"1"],@"addressid":self.addressModel.addressid,@"remarks":@""};
       [NetWorkTools postConJSONWithUrl:@"exchangeintegralgoods" parameters:pamas success:^(id responseObject) {
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
           NSString *ordernum = responseObject[@"ordernum"];
          
           if (result.integerValue == 0) {
              
               BkDuihuanSucessViewController *vc = [[BkDuihuanSucessViewController alloc]init];
               [self.navigationController pushViewController:vc animated:YES];
               vc.callBackBlock = ^{
                   [self.navigationController popToRootViewControllerAnimated:YES];
               };
           }else{
                
           }
           [MBProgressHUD showError:resultNote];
       } fail:^(NSError *error) {
           
       }];
}

@end
